<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">{{
          t('routes.table.roleManagement.addRole')
        }}</a-button>
      </template>
      <!-- 创建时间 -->
      <template #createTime="{ text: createTime }">
        <span>
          {{ proxy.$filters.filterTime(createTime) }}
        </span>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: t('common.confirmDelete'),
                confirm: handleDelete.bind(null, record),
              },
            },
            {
              icon: 'ion:person-outline',
              tooltip: t('routes.table.roleManagement.adjustPerson'),
              onClick: handleAdjustPerson.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <!-- 人员调整 -->
    <PersonAdjustModal @register="registerPersonAdjustModal" />
  </div>
</template>

<script lang="ts" setup>
  import { getCurrentInstance } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { GetAllFunctionRole, DeleteFunRole } from '/@/api/demo/system';
  import { useModal } from '/@/components/Modal';

  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import PersonAdjustModal from '/@/views/components/PersonAdjustModal.vue';

  import { columns, searchFormSchema } from './role.data';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const { proxy }: any = getCurrentInstance();
  const { t } = useI18n();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerPersonAdjustModal, { openModal: openPersonAdjustModal }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: t('routes.table.roleManagement.roleList'),
    api: GetAllFunctionRole,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 200,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    DeleteFunRole(record.id).then((res) => {
      if (res) {
        createMessage.success(t('common.delSuccessText'));
        reload();
      }
    });
  }

  const handleAdjustPerson = (record: Recordable) => {
    openPersonAdjustModal(true, {
      orgId: record.id,
      belongType: '角色',
    });
  };

  function handleSuccess() {
    reload();
  }
</script>
